/**
 * 
 */
package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.manage.model.PhoneAddress;
import com.manage.config.Config;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class PhoneAddressServiceImpl extends AbstractService implements PhoneAddressService
{
	private Logger logger;
	
	public PhoneAddressServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}

	@Override
	public PhoneAddress getPhoneAddress(int id) {
		String sql = "select * from phoneaddress where id=" + String.valueOf(id) + "";
		ModelGenerator<PhoneAddress> dbcpmg = new ModelGenerator<PhoneAddress>(PhoneAddress.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<PhoneAddress> getPhoneAddresss() {
		String sql = "select * from phoneaddress order by id";
		logger.debug("sql: " + sql);
		ModelGenerator<PhoneAddress> dbcpmg = new ModelGenerator<PhoneAddress>(PhoneAddress.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public boolean savePhoneAddress(Account operator, String type, PhoneAddress model) {
		if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		
		String sql = "";
		if (type.equalsIgnoreCase("insert"))
		{
			model.setId(getTableId("phoneaddress"));
			sql = 
				"insert into phoneaddress(id,upaddress,sendcontent,remark) " +
				"values(" + String.valueOf(model.getId()) + ",'" + encodeDb(model.getUpaddress()) + 
				"','" + encodeDb(model.getSendcontent()) + "','" +
						encodeDb(model.getRemark()) + "')";
			
		}
		else if (type.equalsIgnoreCase("update"))
		{
			sql = 
				"update phoneaddress set upaddress='" + encodeDb(model.getUpaddress()) + "'," +
						"sendcontent='" + encodeDb(model.getSendcontent()) + "'," +
						"remark='" + encodeDb(model.getRemark()) + "' " +
						" where id=" + String.valueOf(model.getId()) + "";
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql = "delete from phoneaddress where id=" + String.valueOf(model.getId()) + "";
		}
		else
		{
			sql = "";
		}
		logger.debug("sql: " + sql);
		if (sql.length() > 0)
		{
			if (ConnectionManager.getInstance().execute(dbcp, sql))
			{
				return true;
			}
		}		
		return false;
	}
}
